/**
 * 
 */
package com.etcfg.etlaunch.ui;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import com.etcfg.etlaunch.ETLauncherApplication;
import com.etcfg.etlaunch.ETServer;
import com.etcfg.etlaunch.ApplicationSetting.SlacMode;

@SuppressWarnings("serial")
public class ConnectToServerAction extends AbstractAction {

	/**
	 * 
	 */
	private final MainWindow mainWindow;
	private ETLauncherApplication application;
	private ETServer etServer;

	public ConnectToServerAction(MainWindow mainWindow,
			ETLauncherApplication application) {
		this.mainWindow = mainWindow;
		this.application = application;
		putValue(Action.NAME, "Connect to server");
		putValue(Action.LONG_DESCRIPTION, "Connects to the selected server");
		putValue(Action.SHORT_DESCRIPTION, "Connects to server");
		ImageIcon icon = new ImageIcon(getClass().getResource(
				"/com/etcfg/etlaunch/ui/icons/input-gaming32.png"));

		putValue(Action.LARGE_ICON_KEY, icon);

		putValue(SMALL_ICON, new ImageIcon(getClass().getResource(
				"/com/etcfg/etlaunch/ui/icons/input-gaming-16.png")));

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (application.getEtLauncher().isGameRunning()) {
			int result = JOptionPane
					.showConfirmDialog(
							this.mainWindow.getFrame(),
							"Looks like another instance of ET is running. Do you want to stop this instance, and start new game?",
							"Problem found", JOptionPane.YES_NO_OPTION);

			if (result == JOptionPane.YES_OPTION) {
				final JDialog dialog = this.mainWindow
						.createMessageDialog("Stoping game process");
				application.getExecutor().execute(new Runnable() {

					@Override
					public void run() {
						application.getEtLauncher().stopGame();
						while (!dialog.isVisible()) {
						}
						SwingUtilities.invokeLater(new Runnable() {

							@Override
							public void run() {
								dialog.setVisible(false);

							}
						});
					}
				});
				dialog.setVisible(true);

				actionPerformed(e);
			}
		}

		if (etServer == null) {
			etServer = mainWindow.getSelectedEtServer();

		}
		if (etServer == null) {
			etServer = mainWindow.showInsertServerIpDialog();
			if (etServer == null) {
				return;
			}
		}

		if (etServer.getServerStatus() == null) {
			try {
				etServer.setServerStatus(application.getChecker().checkStatus(
						etServer.getIp(), etServer.getPort(), false));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		if ((etServer.getServerStatus() == null || etServer.getServerStatus()
				.getMod() == null)
				&& (etServer.getLastServerStatus() == null || etServer
						.getLastServerStatus().getMod() == null)) {
			if (application.getApplicationSetting().getSlacMode() == SlacMode.AUTOMATIC) {
				int result = JOptionPane.showOptionDialog(
						mainWindow.getFrame(), "Can't find servertype.", "",
						JOptionPane.YES_NO_CANCEL_OPTION,
						JOptionPane.QUESTION_MESSAGE, null,
						new Object[] { "Connect with SLAC", "Without SLAC",
								"Cancel" }, null);

				if (result == 0 || result == 1) {
					if (result == 0) {
						application.getEtLauncher().setForceSlac(true);
					}
					application.getEtLauncher().startGame(etServer);

				}

			}
			etServer = null;
			return;
		}

		if (etServer.getServerStatus().isPasswordProtected()
				&& etServer.getPassword() == null) {
			String password = JOptionPane.showInputDialog("Server password");
			if (password != null) {
				etServer.setPassword(password);
				application.saveServerList();
			} else {
				return;
			}

		}

		application.getEtLauncher().startGame(etServer);
		etServer = null;

	}

	public ETServer getEtServer() {
		return etServer;
	}

	public void setEtServer(ETServer etServer) {
		this.etServer = etServer;
	}

}